﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Apresentacao;
using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class MDIParentMain : Form
    {
        private int childFormNumber = 0;
        private Form formCadastroParametro = null;
        private Form formCadastroEmpresa = null;

        private Form formSetupImpressora = null;

        private Form formCadastroCliente = null;
        private Form formCadastroMensalista = null;
        private Form formCadastroConvenio = null;

        private Form formCadastroUsuario = null;
        private Form formCadastroPerfil = null;
        private Form formCadastroObjeto = null;
        private Form formCadastroCor = null;

        private Form formCadastroTipoColisao = null;
        private Form formCadastroTipoVeiculo = null;
        private Form formCadastroPorteVeiculo = null;
        private Form formCadastroMarca = null;
        private Form formCadastroModelo = null;

        private Form formCadastroFeriado = null;
        private Form formCadastroOutraData = null;
        private Form formCadastroHorario = null;
        private Form formCadastroTabelaAvulso = null;
        private Form formCadastroTabelaMensal = null;
        private Form formCadastroTabelaConvenio = null;

        private Form formCadastroAlerta = null;
        private Form formCadastroPatio = null;
        private Form formCadastroCabine = null;
        private Form formCadastroFormaPagamento = null;

        private Form formCadastroES = null;

        private Form formRankMarcaModelo = null;
        private Form formCobrancaConvenio = null;
        private Form formContasAReceber = null;
        private Form formContasAPagar = null;
        private Form formPendencia = null;
        private Form formVerificacaoDiaria = null;
        private Form formFechamentoDiario = null;
        private Form formManutencaoES = null;
        private Form formCabineFinanceiro = null;
        private Form formManutencaoFranquia = null;

        private Form formDiario = null;
        private Form formMensal = null;


        private Form formReportMensalista = null;
        private Form formReportPlaca = null;


        private Form formReportEMensal = null;
        private Form formReportEMensalAnalitico = null;
        private Form formReportEDia = null;
        private Form formReportEMes = null;
        
        public MDIParentMain()
        {
            InitializeComponent();
        }

        UsuarioVO usuarioVO = null;

        private void MDIParentMain_Load(object sender, EventArgs e)
        {
            FormLogin formx = new FormLogin();
            formx.ShowDialog(this);
            this.usuarioVO = formx.getUsuarioVO(); //ModelBO.getUsuario("User1",false, true);

            if (usuarioVO == null)
            {
                this.Close();
            }
            if (usuarioVO.idUsuario == -1000)
            {
                return;
            }

            foreach (ToolStripMenuItem m in this.menuStrip.Items)
            {
                if (m.Tag != null)
                {
                    if (m.Tag.ToString().StartsWith("obj="))
                    {
                        m.Visible = false;

                        string obj = m.Tag.ToString().Substring(4);
                        ObjetoVO objetoVO = ModelBO.getObjeto(null, obj);
                        if (objetoVO != null)
                        {
                            foreach (PerfilxUsuarioVO pfVO in usuarioVO.perfilxUsuarioVOs)
                            {
                                PerfilVO pvo = PerfilVO.CreatePerfilVO();
                                pvo.idPerfil = pfVO.idPerfil;
                                ModelBO.getPerfilXObjetos(pvo);
                                PerfilxObjetoVO poVO = pvo.perfilXObjetoVOs.Find(p => p.idObjeto == objetoVO.idObjeto);
                                if (poVO != null)
                                {
                                    m.Visible = poVO.visivel;
                                    m.Enabled = poVO.habilitado;
                                }

                            }
                                 
                            UsuarioxObjetoVO uoVO = usuarioVO.usuarioXObjetoVOs.Find(o => o.idObjeto == objetoVO.idObjeto);
                            if (uoVO != null)
                            {
                                m.Visible = uoVO.visivel;
                                m.Enabled = uoVO.habilitado;
                            }
                        }

                    }
                }
                foreach (ToolStripItem mc in m.DropDownItems)
                {
                    if (mc.Tag != null)
                    {
                        if (mc.Tag.ToString().StartsWith("obj="))
                        {
                            mc.Visible = false;

                            string obj = mc.Tag.ToString().Substring(4);
                            ObjetoVO objetoVO = ModelBO.getObjeto(null, obj);
                            if (objetoVO != null)
                            {
                                foreach (PerfilxUsuarioVO pfVO in usuarioVO.perfilxUsuarioVOs)
                                {
                                    PerfilVO pvo = PerfilVO.CreatePerfilVO();
                                    pvo.idPerfil = pfVO.idPerfil;
                                    ModelBO.getPerfilXObjetos(pvo);
                                    PerfilxObjetoVO poVO = pvo.perfilXObjetoVOs.Find(p => p.idObjeto == objetoVO.idObjeto);
                                    if (poVO != null)
                                    {
                                        mc.Visible = poVO.visivel;
                                        mc.Enabled = poVO.habilitado;
                                    }

                                }

                                UsuarioxObjetoVO uoVO = usuarioVO.usuarioXObjetoVOs.Find(o => o.idObjeto == objetoVO.idObjeto);
                                if (uoVO != null)
                                {
                                    mc.Visible = uoVO.visivel;
                                    mc.Enabled = uoVO.habilitado;
                                }
                            }
                        }
                    }
                }
                
            //    this.textBox1.AppendText(m.GetType().ToString() + m.Text + " " + m.Tag + "\r\n");
            //    if (m.HasDropDownItems)
            //    {
            //        foreach (ToolStripItem mc in m.DropDownItems)
            //        {
            //            if (mc is ToolStripMenuItem)
            //            {
            //                this.textBox1.AppendText("\t" + mc.GetType().ToString() + mc.Text + " " + mc.Tag + "\r\n");
            //            }
            //        }
            //    }
            }
            //this.textBox1.AppendText("\r\n================================================\r\n");
            
            //showControls(this.Controls, "");

        }

        private void usuáriosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroUsuario == null || this.formCadastroUsuario.IsDisposed)
            {
                formCadastroUsuario = new FormCadastroUsuario();
                formCadastroUsuario.MdiParent = this;
            }
            formCadastroUsuario.Show();
        }

        private void perfisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroPerfil == null || this.formCadastroPerfil.IsDisposed)
            {
                formCadastroPerfil = new FormCadastroPerfil();
                formCadastroPerfil.MdiParent = this;
            }
            formCadastroPerfil.Show();

        }

        private void objetosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroObjeto == null || this.formCadastroObjeto.IsDisposed)
            {
                formCadastroObjeto = new FormCadastroObjeto();
                formCadastroObjeto.MdiParent = this;
            }
            formCadastroObjeto.Show();

        }

        private void coresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroCor == null || this.formCadastroCor.IsDisposed)
            {
                formCadastroCor = new FormCadastroCor();
                formCadastroCor.MdiParent = this;
            }
            formCadastroCor.Show();

        }

        private void tipoDeColisõesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroTipoColisao == null || this.formCadastroTipoColisao.IsDisposed)
            {
                formCadastroTipoColisao = new FormCadastroTipoColisao();
                formCadastroTipoColisao.MdiParent = this;
            }
            formCadastroTipoColisao.Show();

        }

        private void tipoDeVeículosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroTipoVeiculo == null || this.formCadastroTipoVeiculo.IsDisposed)
            {
                formCadastroTipoVeiculo = new FormCadastroTipoVeiculo();
                formCadastroTipoVeiculo.MdiParent = this;
            }
            formCadastroTipoVeiculo.Show();

        }

        private void porteDeVeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroPorteVeiculo == null || this.formCadastroPorteVeiculo.IsDisposed)
            {
                formCadastroPorteVeiculo = new FormCadastroPorteVeiculo();
                formCadastroPorteVeiculo.MdiParent = this;
            }
            formCadastroPorteVeiculo.Show();

        }

        private void marcasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroMarca == null || this.formCadastroMarca.IsDisposed)
            {
                formCadastroMarca = new FormCadastroMarca();
                formCadastroMarca.MdiParent = this;
            }
            formCadastroMarca.Show();

        }

        private void modelosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroModelo == null || this.formCadastroModelo.IsDisposed)
            {
                formCadastroModelo = new FormCadastroModelo();
                formCadastroModelo.MdiParent = this;
            }
            formCadastroModelo.Show();

        }

        private void parâmetrosDoSistemaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroParametro == null || this.formCadastroParametro.IsDisposed)
            {
                formCadastroParametro = new FormCadastroParametro();
                formCadastroParametro.MdiParent = this;
            }
            formCadastroParametro.Show();

        }

        private void empresaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroEmpresa == null || this.formCadastroEmpresa.IsDisposed)
            {
                formCadastroEmpresa = new FormCadastroEmpresa();
                formCadastroEmpresa.MdiParent = this;
            }
            formCadastroEmpresa.Show();

        }

        private void feriadoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroFeriado == null || this.formCadastroFeriado.IsDisposed)
            {
                formCadastroFeriado = new FormCadastroFeriado();
                formCadastroFeriado.MdiParent = this;
            }
            formCadastroFeriado.Show();

        }

        private void outrasDatasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroOutraData == null || this.formCadastroOutraData.IsDisposed)
            {
                formCadastroOutraData = new FormCadastroOutraData();
                formCadastroOutraData.MdiParent = this;
            }
            formCadastroOutraData.Show();

        }

        private void horárioDeFuncionamentoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroHorario == null || this.formCadastroHorario.IsDisposed)
            {
                formCadastroHorario = new FormCadastroHorario();
                formCadastroHorario.MdiParent = this;
            }
            formCadastroHorario.Show();

        }

        private void tabelaDePreçosAvulsoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroTabelaAvulso == null || this.formCadastroTabelaAvulso.IsDisposed)
            {
                formCadastroTabelaAvulso = new FormCadastroTabelaAvulso();
                formCadastroTabelaAvulso.MdiParent = this;
            }
            formCadastroTabelaAvulso.Show();

        }

        private void tabelaDePreçosMensalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroTabelaMensal == null || this.formCadastroTabelaMensal.IsDisposed)
            {
                formCadastroTabelaMensal = new FormCadastroTabelaMensal();
                formCadastroTabelaMensal.MdiParent = this;
            }
            formCadastroTabelaMensal.Show();

        }

        private void clienteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroCliente == null || this.formCadastroCliente.IsDisposed)
            {
                formCadastroCliente = new FormCadastroCliente();
                formCadastroCliente.MdiParent = this;
            }
            formCadastroCliente.Show();
        }

        private void operaçãoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroES == null || this.formCadastroES.IsDisposed)
            {
                formCadastroES = new FormCadastroES();
               // formCadastroES.MdiParent = this;
            }
            formCadastroES.Tag = this.usuarioVO;
            formCadastroES.Show();
            //formCadastroES.TopLevel = true;
            //formCadastroES.TopMost = true;
            formCadastroES.Activate();

        }

        private void mensalistaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroMensalista == null || this.formCadastroMensalista.IsDisposed)
            {
                formCadastroMensalista = new FormCadastroMensalista();
                formCadastroMensalista.MdiParent = this;
            }
            formCadastroMensalista.Show();
        }

        private void tabelaDePreçosConvênioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroTabelaConvenio == null || this.formCadastroTabelaConvenio.IsDisposed)
            {
                formCadastroTabelaConvenio = new FormCadastroTabelaConvenio();
                formCadastroTabelaConvenio.MdiParent = this;
            }
            formCadastroTabelaConvenio.Show();
        }

        private void convênioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroConvenio == null || this.formCadastroConvenio.IsDisposed)
            {
                formCadastroConvenio = new FormCadastroConvenio();
                formCadastroConvenio.MdiParent = this;
            }
            formCadastroConvenio.Show();
        }

        private void alertaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroAlerta == null || this.formCadastroAlerta.IsDisposed)
            {
                formCadastroAlerta = new FormCadastroAlerta();
                formCadastroAlerta.MdiParent = this;
            }
            formCadastroAlerta.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.formCadastroES == null || this.formCadastroES.IsDisposed)
            {
                formCadastroES = new FormCadastroES();
                // formCadastroES.MdiParent = this;
            }
            formCadastroES.Show();
            formCadastroES.TopLevel = true;
            formCadastroES.TopMost = true;
            formCadastroES.Activate();

        }

        private void rankingMarcaModeloToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formRankMarcaModelo == null || this.formRankMarcaModelo.IsDisposed)
            {
                formRankMarcaModelo = new FormRankMarcaModelo();
                formRankMarcaModelo.MdiParent = this;
            }
            formRankMarcaModelo.Show();
        }

        private void relatórioCobrançaConvênioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCobrancaConvenio == null || this.formCobrancaConvenio.IsDisposed)
            {
                formCobrancaConvenio = new FormCobrancaConvenio();
                formCobrancaConvenio.MdiParent = this;
            }
            formCobrancaConvenio.Show();
        }

        private void contasAReceberToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formContasAReceber == null || this.formContasAReceber.IsDisposed)
            {
                formContasAReceber = new FormContasAReceber();
                formContasAReceber.MdiParent = this;
            }
            formContasAReceber.Show();
        }

        private void contasAPagarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formContasAPagar == null || this.formContasAPagar.IsDisposed)
            {
                formContasAPagar = new FormContasAPagar();
                formContasAPagar.MdiParent = this;
            }
            formContasAPagar.Show();
        }

        private void cabineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroCabine == null || this.formCadastroCabine.IsDisposed)
            {
                formCadastroCabine = new FormCadastroCabine();
                formCadastroCabine.MdiParent = this;
            }
            formCadastroCabine.Show();
        }

        private void pátioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroPatio == null || this.formCadastroPatio.IsDisposed)
            {
                formCadastroPatio = new FormCadastroPatio();
                formCadastroPatio.MdiParent = this;
            }
            formCadastroPatio.Show();
        }

        private void verificacaoDiáriaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formDiario == null || this.formDiario.IsDisposed)
            {
                formDiario = new FormDiario();
                formDiario.MdiParent = this;
            }
            formDiario.Show();
        }

        private void fechamentoDiárioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formFechamentoDiario == null || this.formFechamentoDiario.IsDisposed)
            {
                formFechamentoDiario = new FormFechamentoDiario();
                formFechamentoDiario.MdiParent = this;
            }
            formFechamentoDiario.Show();
        }

        private void manutençãoDeESToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formManutencaoES == null || this.formManutencaoES.IsDisposed)
            {
                formManutencaoES = new FormManutencaoES();
                formManutencaoES.MdiParent = this;
            }
            formManutencaoES.Show();
        }

        private void setupImpressoraDarumaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formSetupImpressora == null || this.formSetupImpressora.IsDisposed)
            {
                formSetupImpressora = new FormSetupImpressora();
                formSetupImpressora.MdiParent = this;
            }
            formSetupImpressora.Show();
        }

        private void formaDePagamentoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCadastroFormaPagamento == null || this.formCadastroFormaPagamento.IsDisposed)
            {
                formCadastroFormaPagamento = new FormCadastroFormaPagamento();
                formCadastroFormaPagamento.MdiParent = this;
            }
            formCadastroFormaPagamento.Show();
        }

        private void movimentoFinanceiroCabineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formCabineFinanceiro == null || this.formCabineFinanceiro.IsDisposed)
            {
                formCabineFinanceiro = new FormCabineFinanceiro();
                formCabineFinanceiro.MdiParent = this;
            }
            formCabineFinanceiro.Show();
        }

        private void relatorioMensalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formMensal == null || this.formMensal.IsDisposed)
            {
                formMensal = new FormMensal();
                formMensal.MdiParent = this;
            }
            formMensal.Show();
        }

        private void pendenciaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formPendencia == null || this.formPendencia.IsDisposed)
            {
                formPendencia = new FormPendencia();
                formPendencia.MdiParent = this;
            }
            formPendencia.Show();
        }

        private void mensalistaToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.formReportMensalista == null || this.formReportMensalista.IsDisposed)
            {
                formReportMensalista = new FormReportMensalista();
                formReportMensalista.MdiParent = this;
            }
            formReportMensalista.Show();
        }

        private void placaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formReportPlaca == null || this.formReportPlaca.IsDisposed)
            {
                formReportPlaca = new FormReportPlaca();
                formReportPlaca.MdiParent = this;
            }
            formReportPlaca.Show();
        }

        private void consolidadoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formReportEMensal == null || this.formReportEMensal.IsDisposed)
            {
                formReportEMensal = new FormReportEMensal();
                formReportEMensal.MdiParent = this;
            }
            formReportEMensal.Show();
        }

        private void analiticoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formReportEMensalAnalitico == null || this.formReportEMensalAnalitico.IsDisposed)
            {
                formReportEMensalAnalitico = new FormReportEMensalAnalitico();
                formReportEMensalAnalitico.MdiParent = this;
            }
            formReportEMensalAnalitico.Show();
        }

        private void fatDiarioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formReportEDia == null || this.formReportEDia.IsDisposed)
            {
                formReportEDia = new FormReportEDia();
                formReportEDia.MdiParent = this;
            }
            formReportEDia.Show();
        }

        private void fatMensalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formReportEMes == null || this.formReportEMes.IsDisposed)
            {
                formReportEMes = new FormReportEMes();
                formReportEMes.MdiParent = this;
            }
            formReportEMes.Show();
        }

        private void manutençãoDeFranquiasDeMensalistaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.formManutencaoFranquia == null || this.formManutencaoFranquia.IsDisposed)
            {
                //formManutencaoFranquia = new FormManutencaoFranquia();
                //formManutencaoFranquia.MdiParent = this;
            }
            //formManutencaoFranquia.Show();
        }

    }
}
